package com.imooc.ioc.demo1;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;

/**
 * Spring的工厂类介绍
 * @author wangc
 */

public class SpringDemo1Test {
    @Test
    /**
     *传统方式开发
     */
    public void demo1() {
        UserService userService = new UserServiceImpl();
        userService.sayHello();
    }

    @Test
    /**
     * Spring的方式实现
     */
    public void demo2() {
        //创建spring的工厂
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext.xml");
        UserService userService = (UserService) applicationContext.getBean("userService");
        userService.sayHello();
    }

    @Test
    /**
     * 读取磁盘系统中的配置文件
     * 把applicationContext.xml复制道D盘下
     */
    public void demo3() {
        //创建Spring的工厂类
        ApplicationContext applicationContext = new FileSystemXmlApplicationContext("D:\\test\\applicationContext.xml");
        //通过工厂获得类
        UserService userService = (UserService) applicationContext.getBean("userService");
        userService.sayHello();
    }

    @Test
    /**
     * 传统方式的工厂类：BeanFactory
     * 只做了解
     */
    public void demo4() {
        //创建工厂类
        BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
        //通过工厂获得类
        UserService userService = (UserService) beanFactory.getBean("userService");
        userService.sayHello();

    }

    @Test
    /**
     * 传统方式的工厂类：BeanFactory
     * 只做了解
     */
    public void demo5() {
        //创建工厂类
        BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("D:\\test\\applicationContext.xml"));
        //通过工厂获得类
        UserService userService = (UserService) beanFactory.getBean("userService");
        userService.sayHello();

    }
}
